<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

use think\Request;

class Protype extends Backend {
    //默认配置

    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->ConfigModel = model( 'Config' );
        $this->UserModel = model( 'User.User' );
        $this->RechargeModel = model( 'User.Recharge' );
        $this->ProductModel = model( 'Product.Product' );
        $this->ProtypeModel = model( 'Product.Type' );
    }

    // 获取商品类别信息

    public function index() {
        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'name' ] = [ 'like', "%$keywords%" ];
        }
        // 查询帖子总数
        $protypeCount = $this->ProtypeModel->count();

        $protypeList = $this->ProtypeModel->where( $where )->order( 'weigh', 'desc' )->paginate( 5, false, [ 'query' => Request::instance()->param() ] );
        $protypeStr = $protypeList->render();

        $this->assign( [
            'protypeList' => $protypeList,
            'protypeStr' => $protypeStr,
            'protypeCount' => $protypeCount,
            'keywords' => $keywords
        ] );

        return $this->fetch();
    }

    // 修改类别

    public function edit() {

        $typeid = $this->request->param( 'id', 0 );

        // 查找数据
        $typeinfo = $this->ProtypeModel->where( [ 'id' => $typeid ] )->find();

        if ( $typeinfo === false ) {
            $this->error( '获取数据失败！' );
            exit;
        }

        $this->assign( [
            'typeinfo' => $typeinfo,
        ] );

        if ( $this->request->isPost() ) {

            // 获取表单数据
            $params = $this->request->param();

            // 组装配置信息
            $data = [
                'id' => $typeid,
                'name' => $params[ 'name' ],
                'weigh' => $params[ 'weigh' ],
            ];

            if ( empty( request()->file( 'cover' ) ) ) {
                $coverImgURL = isset( $params[ 'cover' ] ) ? $params[ 'cover' ] : $typeinfo[ 'cover' ];
            } else {
                // 背景图片上传
                $coverImgURL = upload( 'cover' );

                if ( $coverImgURL === false ) {
                    $this->error( '修改背景失败！' );
                    exit;
                }

                $coverImgURL = 'uploads/'.$coverImgURL;
            }

            $data[ 'cover' ] = $coverImgURL;

            // 分类表插入数据
            $result = $this->ProtypeModel->isUpdate()->save( $data );

            if ( $result === false ) {
                $this->error( $this->ProtypeModel->getError() );
                exit;
            } else {
                $this->success( '商品分类信息更新成功！', 'admin/protype/index' );
                exit;
            }
        }

        return $this->fetch();
    }

    // 检测输入是否正确

    public function check() {
        $request = [
            'result' => false,
            'msg' => '',
        ];

        // 获取输入的分类名
        $name = $this->request->param( 'name' );

        // 获取全部分类信息
        $protypeList = $this->ProtypeModel->where( [ 'name' => $name ] )->find();

        if ( $protypeList ) {
            $request[ 'result' ] = false;
            $request[ 'msg' ] = '分类名已经存在，请重新输入！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        } else {
            $request[ 'result' ] = true;
            $request[ 'msg' ] = '分类名不存在，可以继续操作！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        }

    }

    // 添加类别

    public function add() {
        if ( $this->request->isPost() ) {
            // 获取表单数据
            $params = $this->request->param();

            // 组装配置信息
            $data = [
                'name' => $params[ 'name' ],
                'weigh' => $params[ 'weigh' ],
            ];

            if ( empty( request()->file( 'cover' ) ) ) {
                $coverImgURL = isset( $params[ 'cover' ] ) ? $params[ 'cover' ] : '';
            } else {
                // 背景图片上传
                $coverImgURL = upload( 'cover' );

                if ( $coverImgURL === false ) {
                    $this->error( '修改背景失败！' );
                    exit;
                }

                $coverImgURL = 'uploads/'.$coverImgURL;
            }

            $data[ 'cover' ] = $coverImgURL;

            // 配置表插入数据
            $result = $this->ProtypeModel->save( $data );

            if ( $result === false ) {
                $this->error( $this->ProtypeModel->getError() );
                exit;
            } else {
                $this->success( '商品分类信息添加成功！', 'admin/protype/index' );
                exit;
            }

        }
        return $this->fetch();
    }

    // 删除

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 删除
            $result = $this->ProtypeModel->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ProtypeModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除分类成功';
            }

            return json( $success );
        }
    }
}
?>